package com.heo.music.player.config;

import lombok.RequiredArgsConstructor;
import org.modelmapper.Converter;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Optional;

/**
 * @author 星天无极
 **/
@Configuration
@RequiredArgsConstructor
public class ModelMapperConfiguration {

    @Bean
    public ModelMapper modelMapper(Optional<List<Converter<?, ?>>> converterList) {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration()
            .setMatchingStrategy(MatchingStrategies.STRICT);

        converterList.ifPresent(converters -> {
            for (Converter<?, ?> converter : converters) {
                modelMapper.addConverter(converter);
            }
        });
        return modelMapper;
    }

}
